<script type="text/javascript">
	var $filesadd_content = $("#filesadd_content");

	$(document).ready(function () {
		var theme = 'fresh';
		$('.field_input').addClass('jqx-input');
		$('.field_input').addClass('field');
		$('.field_input').addClass('jqx-rc-all');

		var filesadd_save = $("#filesadd_save");
		var filesadd_cancel = $("#filesadd_cancel");
		var filepost_id = $("#filepost_id");
		var filepost_task = $("#filepost_task");
		var filepost_judul = $("#filepost_judul");
		var filepost_kategori = $("#filepost_kategori");
		var filepost_tanggal = $("#filepost_tanggal");
		var filepost_file = $("#filepost_file");
		var filepost_filename = $("#filepost_filename");
		var filepost_tag = $("#filepost_tag");
		var filepost_keterangan = $("#filepost_keterangan");
		var filepost_publish = $("#filepost_publish");

		var kategori_source = {
			id : 'id',
			datatype : "json",
			url : "index.php/c_kategori/kategori_list",
			root : "results",
			datafields : [
				{ name : 'id' },
				{ name : 'kategori' }
			],
			beforeprocessing : function(data){
				kategori_source.totalrecords = data[0].TotalRows;
			}
		}
		var kategori_dataAdapter = new $.jqx.dataAdapter(kategori_source,{
			loadComplete: function (){
				<?php
				if(isset($dataDetail)){
				?>
					filepost_kategori.find('input').val('<?php echo $dataDetail->kategori; ?>');
				<?php } ?>
			}
		});

		filesadd_save.jqxButton({ theme : theme, width : '70px', height : '18px' });
		filesadd_cancel.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		filepost_kategori.jqxComboBox({
			source : kategori_dataAdapter,
			theme : theme,
			width : 200,
			height : 25,
			autoDropDownHeight: true,
			displayMember : "kategori",
			valueMember : "id"
		});

		filepost_tanggal.jqxDateTimeInput({
			formatString : 'dd-MM-yyyy',
			width : 150,
			height : 25,
			theme : theme,
			enableAbsoluteSelection: true
		});
		filepost_file.fileupload({
			dataType: 'json',
			formData : function(form){
				return [{ name: 'kode', value: 'asdasd' }];
			},
			done: function (e, data){
				var text = "";
				$.each(data.result, function (index, file) {
					text = text + file.name;
					$("#filepost_filename").val(text);
				});
			}
		});
		/* function */
		filesadd_cancel.click(function(){
			$('#mainpanelcontent').empty();
			$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#mainpanelcontent').load('index.php/c_files/file_load');
		});
		filesadd_save.click(function(){
			var id = "";
			var task = "";
			var judul = "";
			var kat_id = "";
			var tanggal = "";
			var file_attach = "";
			var tag = "";
			var keterangan = "";
			var publish = "";

			id = filepost_id.val();
			task = filepost_task.val();
			judul = filepost_judul.val();
			if(filepost_kategori.jqxComboBox('getSelectedItem') !== null){
				kat_id = filepost_kategori.jqxComboBox('getSelectedItem').value;
			}
			tanggal = filepost_tanggal.jqxDateTimeInput('getDate');
			tanggal = $.jqx.dataFormat.formatdate(tanggal, 'yyyy-MM-dd');
			file_attach = filepost_filename.val();
			tag = filepost_tag.val();
			keterangan = filepost_keterangan.val();
			if (filepost_publish.prop("checked")==true) {
				publish = 1;
			}else{
				publish = 0;
			}

			if(task == ""){
				task = "create";
			}
			$.ajax({
				type : 'POST',
				url : 'index.php/c_files/file_save',
				data : {
					id : id,
					kat_id : kat_id,
					judul : judul,
					tanggal : tanggal,
					file_attach : file_attach,
					tag : tag,
					keterangan : keterangan,
					publish : publish,
					task : task
				},success : function(msg){
					var result = eval(msg);
					switch(result){
						case 1:
							$('#mainpanelcontent').empty();
							$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
							$('#mainpanelcontent').load('index.php/c_files/file_load');
							alert("Data berhasil disimpan");
							break;
						default:
							alert("Data gagal disimpan");
							break;
					}
				}
			});

		});

		<?php
			if(isset($dataDetail)){
		?>
			filepost_id.val('<?php echo $dataDetail->id; ?>');
			filepost_judul.val('<?php echo $dataDetail->judul; ?>');
			var arr_tgl = '<?php echo $dataDetail->tanggal; ?>'.split('-');
			var set_tgl = new Date(arr_tgl[0], (arr_tgl[1] - 1), arr_tgl[2]);
			filepost_tanggal.jqxDateTimeInput('setDate', set_tgl);
			filepost_tag.val('<?php echo $dataDetail->tag; ?>');
			filepost_filename.val('<?php echo $dataDetail->file_attach; ?>');
			filepost_keterangan.text('<?php echo $dataDetail->keterangan; ?>');
			filepost_task.val('update');
		<?php
			$this->firephp->log($dataDetail->publish);
				if($dataDetail->publish==1){
					echo "filepost_publish.prop(\"checked\", true);";
				}else{
					echo "filepost_publish.prop(\"checked\", false);";
				}
			}
		?>

	});

</script>
<!-- Begin content -->
<div class="modal_header">
		<span>Files Form</span>
	</div>
<div class="modal_content">
<div id="filesadd_content">
	<form action="" method="post" >
		<input type="hidden" id="filepost_id">
		<input type="hidden" id="filepost_task">
		<p>
			<label>Judul:</label><br/>
			<input type="text" id="filepost_judul" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Kategori:</label><br/>
			<div id="filepost_kategori"></div>
		</p>
		<br/>
		<p>
			<label>Tanggal:</label><br/>
			<div id="filepost_tanggal"></div>
		</p>
		<br/>
		<p>
			<label>File:</label><br/>
			<input type="file" id="filepost_file" name="userfile" style="width:300px" data-url="index.php/upload/upload_img" />
			<input type="hidden" id="filepost_filename"/>
		</p>
		<br/>
		<p>
			<label>Tag:</label><br/>
			<input type="text" id="filepost_tag" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Keterangan:</label><br/>
			<textarea rows="7" cols="50" id="filepost_keterangan"></textarea>
		</p>
		<br/>

		<p>
			<input type="checkbox" class="checkbox" checked="checked" id="filepost_publish"> <label for="cbdemo1">Publish</label>
		</p>
		<br/><br/>
		<p><table align="left">
			<tr align="left">
				<td>
					<div id='filesadd_save'>
					<img style='float: left; margin: 0px;' src='./images/icons/save.gif' />
					<div style='float: left; margin-left: 4px;'>Simpan</div>
				</div>
				</td>
				<td>
					<div id='filesadd_cancel'>
					<img style='float: left; margin: 0px;' src='./images/icons/cross.gif' />
					<div style='float: left; margin-left: 4px;'>Cancel</div>
				</div>
				</td>
			</tr>
		</table>

		</p><p>&nbsp;</p>
	</form>
</div>
</div>
<!-- End left column window -->
